package com.soilflow.web.controller.dahu.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soilflow.common.annotation.Log;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.SysDictData;
import com.soilflow.common.core.domain.entity.SysUser;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.*;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.SecurityUtils;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.service.IDahuConvenientService;
import com.soilflow.info.service.IDahuJobhuntService;
import com.soilflow.info.service.IDahuRecruitService;
import com.soilflow.info.service.IDahuRouteStopService;
import com.soilflow.system.service.IDahuBusinessService;
import com.soilflow.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商家Controller
 * 
 * @author party
 * @date 2023-10-26
 */
@RestController
@RequestMapping("/reception/business")
public class BusinessController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private IDahuBusinessService dahuBusinessService;
    @Autowired
    private IDahuRecruitService recruitService;
    @Autowired
    private IDahuJobhuntService jobhuntService;
    @Autowired
    private IDahuRouteStopService routeStopService;
    @Autowired
    private IDahuConvenientService convenientService;

    /**
     * 查询商家列表
     */
//    @PreAuthorize("@ss.hasPermi('system:business:list')")
    @GetMapping("/list")
    public TableDataInfo list(DahuBusiness dahuBusiness)
    {
        startPage();
        List<DahuBusiness> list = dahuBusinessService.selectDahuBusinessList(dahuBusiness);
        return getDataTable(list);
    }


    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 获取商家详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dahuBusinessService.selectDahuBusinessById(id));
    }

    /**
     * 新增商家
     */
//    @PreAuthorize("@ss.hasPermi('system:business:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DahuBusiness dahuBusiness)
    {
        return toAjax(dahuBusinessService.insertDahuBusiness(dahuBusiness));
    }

    /**
     * 商家申请入驻
     */
    @PutMapping("/settleIn")
    public AjaxResult settleIn(@RequestBody DahuBusiness dahuBusiness)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        System.out.println("user = " + user);
        dahuBusiness.setCreateId(user.getUserId());
        dahuBusiness.setCreateBy(user.getUserName());
        return toAjax(dahuBusinessService.settleIn(dahuBusiness));
    }

    /**
     * pc端商家申请入驻
     */
    @PostMapping("/applyFor")
    public AjaxResult applyFor(@RequestBody DahuBusiness dahuBusiness)
    {   
        SysUser user = SecurityUtils.getLoginUser().getUser();
        LambdaQueryWrapper<DahuBusiness> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuBusiness::getCreateId,user.getUserId());
        DahuBusiness business = dahuBusinessService.getOne(wrapper);
        if (business!=null){
            return AjaxResult.error("您以是商家了");
        }else {
            dahuBusiness.setCreateBy(user.getUserName());
            dahuBusiness.setCreateId(user.getUserId());
            return toAjax(dahuBusinessService.save(dahuBusiness));
        }
    }



    /**
     * 修改商家
     */
    @PreAuthorize("@ss.hasPermi('system:business:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DahuBusiness dahuBusiness)
    {
        return toAjax(dahuBusinessService.updateDahuBusiness(dahuBusiness));
    }

    /**
     * 删除商家
     */
    @PreAuthorize("@ss.hasPermi('system:business:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dahuBusinessService.deleteDahuBusinessByIds(ids));
    }
    /**
     * 获取当前用户全部招聘信息
     */
    @GetMapping("/getuserrecruiit/{userId}")
    public TableDataInfo getuserrecruiit(@PathVariable("userId") Long userId) {
        startPage();
        LambdaQueryWrapper<DahuRecruit> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuRecruit::getCreateId,userId);
        List<DahuRecruit> list = recruitService.list(wrapper);
        return getDataTable(list);
    }
    /**
     * 获取当前用户全部求职信息
     */
    @GetMapping("/getuserJobhunt/{userId}")
    public TableDataInfo getuserJobhunt(@PathVariable("userId") Long userId) {
        startPage();
        LambdaQueryWrapper<DahuJobhunt> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuJobhunt::getCreateId,userId);
        List<DahuJobhunt> list = jobhuntService.list(wrapper);
        return getDataTable(list);
    }
    /**
     * 获取当前用户全部发布拼车信息
     */
    @GetMapping("/getuserRouteStop/{userId}")
    public TableDataInfo getuserRouteStop(@PathVariable("userId") Long userId) {
        startPage();
        LambdaQueryWrapper<DahuRouteStop> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuRouteStop::getCreateId,userId);
        List<DahuRouteStop> list = routeStopService.list(wrapper);
        return getDataTable(list);
    }
    /**
     * 获取当前用户全部发布便民信息
     */
    @GetMapping("/getuserconvenient/{userId}")
    public TableDataInfo getuserconvenient(@PathVariable("userId") Long userId) {
        startPage();
        LambdaQueryWrapper<DahuConvenient> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuConvenient::getCreateId,userId);
        List<DahuConvenient> list = convenientService.list(wrapper);
        return getDataTable(list);
    }
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        return ajax;
    }
    @GetMapping("/orbusiness")
    public AjaxResult orbusiness() {
        LambdaQueryWrapper<DahuBusiness> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuBusiness::getCreateId,SecurityUtils.getUserId());
        DahuBusiness one = dahuBusinessService.getOne(wrapper);
        System.out.println("one = " + one);
        if (one==null){
            return AjaxResult.success(0);
        }else {
            return AjaxResult.success(1);
        }
    }
    @GetMapping("/getbusiness")
    public AjaxResult getbusiness() {
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        LambdaQueryWrapper<DahuBusiness> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DahuBusiness::getCreateId,user.getUserId());
        DahuBusiness business = dahuBusinessService.getOne(wrapper);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("business", business);
        return ajax;
    }
}
